"""
后台管理订单序列化器
"""
from rest_framework import serializers
from pecco_site.models import Order, OrderItem, OrderStatusHistory


class AdminOrderItemSerializer(serializers.ModelSerializer):
    """后台订单项目序列化器"""
    subtotal = serializers.SerializerMethodField()
    product_id = serializers.IntegerField(source='product.id', read_only=True, allow_null=True)
    variant_id = serializers.IntegerField(source='variant.id', read_only=True, allow_null=True)
    
    class Meta:
        model = OrderItem
        fields = [
            'id', 'product_id', 'variant_id',
            'product_name', 'variant_name',
            'unit_price', 'quantity', 'subtotal',
            'created_at'
        ]
        read_only_fields = ['id', 'created_at']
    
    def get_subtotal(self, obj):
        return obj.get_subtotal()


class AdminOrderStatusHistorySerializer(serializers.ModelSerializer):
    """后台订单状态历史序列化器"""
    
    class Meta:
        model = OrderStatusHistory
        fields = ['id', 'status', 'note', 'created_at']
        read_only_fields = ['id', 'created_at']


class AdminOrderSerializer(serializers.ModelSerializer):
    """后台订单序列化器"""
    items = AdminOrderItemSerializer(many=True, read_only=True)
    status_history = AdminOrderStatusHistorySerializer(many=True, read_only=True)
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    user_email = serializers.EmailField(source='user.email', read_only=True, allow_null=True)
    user_username = serializers.CharField(source='user.username', read_only=True, allow_null=True)
    
    class Meta:
        model = Order
        fields = [
            'id', 'order_number', 'status', 'status_display',
            'user', 'user_email', 'user_username',
            'shipping_full_name', 'shipping_company',
            'shipping_address_line1', 'shipping_address_line2',
            'shipping_city', 'shipping_state', 'shipping_postal_code',
            'shipping_country', 'shipping_phone',
            'subtotal_price', 'shipping_cost', 'total_price',
            'note', 'admin_note', 'tracking_number',
            'items', 'status_history',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'order_number', 'created_at', 'updated_at']


class AdminOrderListSerializer(serializers.ModelSerializer):
    """后台订单列表序列化器"""
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    user_email = serializers.EmailField(source='user.email', read_only=True, allow_null=True)
    items_count = serializers.SerializerMethodField()
    
    class Meta:
        model = Order
        fields = [
            'id', 'order_number', 'status', 'status_display',
            'user_email', 'shipping_full_name',
            'total_price', 'items_count',
            'tracking_number', 'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'order_number', 'created_at', 'updated_at']
    
    def get_items_count(self, obj):
        return obj.items.count()


class UpdateOrderStatusSerializer(serializers.Serializer):
    """更新订单状态序列化器"""
    status = serializers.ChoiceField(choices=Order.ORDER_STATUS)
    note = serializers.CharField(required=False, allow_blank=True, default='')
    tracking_number = serializers.CharField(required=False, allow_blank=True, default='')

